//
//  ResetPasswordViewController.m
//  beautyLamp
//
//  Created by Tam Kwok To Michael on 2017/8/9.
//  Copyright © 2017年 Tam Kwok To Michael. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import <MBProgressHUD.h>
#import "VervifyPhone.h"
#import "LoginNet.h"
#import "NSString+SortNameUtils.h"

@interface ResetPasswordViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *telField;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UIView *topView;
@property (weak, nonatomic) IBOutlet UITextField *commitPwdField;
@property (strong, nonatomic) NSTimer *timer;
@property (nonatomic, assign) NSInteger time;
@property (weak, nonatomic) IBOutlet UIButton *codeBtn;

@property (weak, nonatomic) IBOutlet UIButton *resetpwdBtn;
@end

@implementation ResetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}

- (void)setupUI {
    self.title = @"重置密码";
    self.topView.layer.cornerRadius = 5;
    self.codeBtn.layer.cornerRadius = 5;
    self.resetpwdBtn.layer.cornerRadius = 5;
    [self.telField becomeFirstResponder];
    UITapGestureRecognizer *hide = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideKeyBoard)];
    [self.view addGestureRecognizer:hide];
    
}

- (void)hideKeyBoard {
    [self.view endEditing:YES];
}


#pragma mark 密码重置
- (IBAction)commitResetPwd:(id)sender {
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    
    NSString *tel = self.telField.text;
    NSString *code = self.codeField.text;
    NSString *pwd = self.pwdField.text;
    NSString *commitPwd = self.commitPwdField.text;
    
    if(tel.length == 0) {
        hud.label.text = @"请输入手机号码！";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(tel.length != 11 || ![VervifyPhone vervifyTellphoneNumber:tel]) {
        hud.label.text = @"手机号码格式不正确";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(code.length == 0) {
        hud.label.text = @"请输入验证码";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }

    if(pwd.length == 0){
        hud.label.text = @"请输入新密码";
        [hud hideAnimated:YES afterDelay:3];
        return;

    }
    if(commitPwd.length == 0){
        hud.label.text = @"请输入确认密码";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(![commitPwd isEqualToString:pwd]) {
        hud.label.text = @"两次密码不一致";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = @"拼命加载中。。。";
    //密码加密
    NSString *pwdMd5 = [pwd md5String];
    NSDictionary *dic = @{@"userTel": tel, @"code": code, @"passWord": pwdMd5};
    __weak typeof (self) weakSelf = self;
    [LoginNet resetPWd:dic completer:^(id resultObject, NSError *error) {
        if(error) {
            hud.mode = MBProgressHUDModeText;
            if(error.code == 0) {
                hud.label.text = [error localizedDescription];
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 2 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
                    [hud hideAnimated:YES];
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
            }else {
                hud.label.text = [error localizedDescription];
                [hud hideAnimated:YES afterDelay:3];
            }
            return;
        }
        
        [hud hideAnimated:YES];
    }];
}


#pragma mark 获取验证码
- (IBAction)getCode:(UIButton *)sender {
    if(self.time > 0) {
        return;
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDModeText;
    NSString *tel = self.telField.text;
    if(tel.length == 0) {
        hud.label.text = @"请输入手机号码！";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    if(tel.length != 11 || ![VervifyPhone vervifyTellphoneNumber:tel]) {
        hud.label.text = @"手机号码格式不正确";
        [hud hideAnimated:YES afterDelay:3];
        return;
    }
    
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.label.text = @"拼命加载中。。。";
    [LoginNet getCodeWithTel:tel codeType:3 completer:^(id resultObject, NSError *error) {
        if(error) {
            hud.mode = MBProgressHUDModeText;
            [hud hideAnimated:YES afterDelay:3];
            hud.label.text = [error localizedDescription];
            return;
        }
    }];
    
    self.time = 120;
    __weak typeof (self) weakSelf = self;
    [sender setTitle:@"120s后重新获取" forState:UIControlStateNormal];
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
        weakSelf.time--;
        if(weakSelf.time == 0) {
            [sender setTitle:@"获取验证码" forState:UIControlStateNormal];
            sender.userInteractionEnabled = YES;
            [weakSelf.timer invalidate];
        }else {
            [sender setTitle:[NSString stringWithFormat:@"%zis后重新获取",weakSelf.time] forState:UIControlStateNormal];
        }
    }];
}


- (IBAction)lookPwd:(UIButton *)sender {
    if(sender.isSelected) {
        sender.selected = NO;
        self.pwdField.secureTextEntry = YES;
    }else {
        sender.selected = YES;
        self.pwdField.secureTextEntry = NO;
    }
}

- (IBAction)lookCommitPwd:(UIButton *)sender {
    if(sender.isSelected) {
        sender.selected = NO;
        self.commitPwdField.secureTextEntry = YES;
    }else {
        sender.selected = YES;
        self.commitPwdField.secureTextEntry = NO;
    }
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [self.timer invalidate];
}

@end
